package leetcode20211006;

import java.util.ArrayDeque;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LUYAO
 * Date: 2021-10-06
 * Time: 20:51
 * 请从字符串中找出一个最长的不包含重复字符的子字符串，计算该最长子字符串的长度。
 *
 *  
 *
 * 示例 1:
 *
 * 输入: "abcabcbb"
 * 输出: 3
 * 解释: 因为无重复字符的最长子串是 "abc"，所以其长度为 3。
 *
 */
public class LongestString {
    public static void main(String[] args) {

    }
}

class Solution2 {
    public int lengthOfLongestSubstring(String s) {
        ArrayDeque<Character> deque = new ArrayDeque<>();
        int max = 0;
        for (int i = 0; i < s.length(); i++) {
            if(!deque.contains(s.charAt(i))){
                deque.addLast(s.charAt(i));
            }else{
                max = Math.max(max,deque.size());
                while(deque.peekFirst() != s.charAt(i)){
                    deque.removeFirst();
                }
                deque.removeFirst();
                deque.addLast(s.charAt(i));
            }

        }

        max = Math.max(max,deque.size());
        return max;

    }
}
